package com.hh.generator.core.generator;

import com.hh.generator.common.dto.ColumnEntity;
import com.hh.generator.common.dto.TableEntity;
import com.hh.generator.core.Generator;
import com.hh.generator.core.model.ClassModel;
import com.hh.generator.core.model.Config;
import com.hh.generator.core.model.FacadeModel;
import com.hh.generator.core.model.MapperModel;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.hh.generator.common.util.TableToClassConvertUtil.*;

public class MapperGenerator extends AbstractTableBaseGenerator implements Generator {

    private String templateName = "Mapper.java.vm";

    public MapperGenerator(Config config, TableEntity tableEntity, List<ColumnEntity> columnEntityList) {
        super(config, tableEntity, columnEntityList);
    }


    @Override
    public String fileName() {
        return mapperClassName(config.getTablePrefix(), tableEntity.getTableName()) + ".java";
    }

    @Override
    public ClassModel buildModel() {
        String infoClassName = infoClassName(config.getTablePrefix(), tableEntity.getTableName());
        String infoClassFullName = config.getInfoPackage().replace("xx", config.getModule()) + "." + infoClassName;
        String entityClassName = className(config.getTablePrefix(), tableEntity.getTableName());
        String entityClassFullName = config.getEntityPackage().replace("xx", config.getModule()) + "." + entityClassName;
        String queryRequestClassName = queryRequestClassName(config.getTablePrefix(), tableEntity.getTableName());
        String queryRequestClassFullName = config.getRequestPackage().replace("xx", config.getModule()) + "." + queryRequestClassName;

        Set<String> imports = new HashSet<>();
        imports.add(entityClassFullName);
        MapperModel model = new MapperModel();
        if (config.getMethods().contains("query")) {
            FacadeModel.Method method = new FacadeModel.Method();
            method.setMethodName("query" + entityClassName);
            method.setRequest(entityClassName + "QueryRequest");
            model.setQueryMethod(method);
            imports.add(config.getPageClassName());
            imports.add(config.getMybatisParamClassName());
            imports.add(infoClassFullName);
            imports.add(queryRequestClassFullName);
        }
        if (config.getMethods().contains("get")) {
            FacadeModel.Method method = new FacadeModel.Method();
            method.setMethodName("get" + entityClassName + "ById");
            model.setGetMethod(method);
            imports.add(config.getMybatisParamClassName());
            imports.add(infoClassFullName);
        }
        if (config.getMethods().contains("list")) {
            FacadeModel.Method method = new FacadeModel.Method();
            method.setMethodName("list" + entityClassName);
            method.setRequest(entityClassName + "QueryRequest");
            model.setListMethod(method);
            imports.add(config.getMybatisParamClassName());
            imports.add(infoClassFullName);
            imports.add(queryRequestClassFullName);
            imports.add(config.getListClassName());
        }
        model.setInfoClassName(infoClassName);
        model.setEntityClassName(entityClassName);
        model.setEntityClassFullName(entityClassFullName);
        model.setPackageName(config.getMapperPackage().replace("xx", config.getModule()));
        model.setImports(imports.stream().sorted().collect(Collectors.toList()));
        model.setComment(tableEntity.getTableComment() + " Mapper接口");
        model.setAuthor(config.getClassAuthor());
        model.setDate(config.getDate());
        model.setClassName(mapperClassName(config.getTablePrefix(), tableEntity.getTableName()));
        model.setClassFullName(model.getPackageName() + "." + model.getClassName());
        return model;
    }

    @Override
    public String getTemplateName() {
        return templateName;
    }

}
